package com.vmap.longitude;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.widget.TextView;
import android.widget.Toast;

public class Proximity extends Activity {
	
	public TextView proximity_distance;
	public TextView endlocation;
	static double startlongitude;
	static double startlatitude;
	static double endLatitude;
	static double endLongitude;
	static double distance ;
	Serializable parentClass;
	
	public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
		 setContentView(R.layout.proximity);
		 Intent sender = getIntent();
		 parentClass = sender.getExtras().getSerializable("parentClass");
		 proximity_distance = (TextView)findViewById(R.id.proximity_distance);
		 proximity_distance.setText("1000");
		 proximity_distance.setTextColor(Color.GRAY);
		 endlocation = (TextView)findViewById(R.id.endlocation);
		 endlocation.setText("Enter Destination");
		 endlocation.setTextColor(Color.GRAY);
		endlocation.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				endlocation = (TextView)findViewById(R.id.endlocation);
	
				 if(endlocation.getText().toString().contentEquals("") && !hasFocus)
					 endlocation.setText("Enter Destination");
				 			
			}
		});
		endlocation.setOnTouchListener(new OnTouchListener(){
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				endlocation = (TextView)findViewById(R.id.endlocation);
				if(endlocation.getText().toString().contentEquals("Enter Destination"))
					 endlocation.setText("");
					
				return false;
			}
		});
		
			
			
	}
	//this is called on Go button click defined in proximity.xml 
	public void addProximity(View view) 
	{
		String proximity=proximity_distance.getText().toString();
		String end = endlocation.getText().toString();
		
		
		if( proximity.contentEquals("") || proximity == null )
		{
			Toast.makeText(this, "please enter the proximity", Toast.LENGTH_LONG).show(); 
			return;
		}
		else
		{
			//CharSequence[] a =  proximity;
			for(int i=0;i < proximity.length();i++)
			{
				if (!( (proximity.charAt(i) >= '0') && (proximity.charAt(i) <= '9')))
				{
					
					Toast.makeText(this, "please enter only numbers", Toast.LENGTH_LONG).show();
					return;
				}
			}
			
			distance = Double.parseDouble(proximity);
		}
		

		if(end.contentEquals(""))
		{
			Toast.makeText(this, "please enter the end point", Toast.LENGTH_LONG).show(); 
			return;
		}
					
		
		Geocoder gc = new Geocoder(this);
	
		try {
			List<Address> addresses = gc.getFromLocationName(end, 5);
			if(addresses.size() > 0)
			{
				//for(int i=0;i<addresses.size();i++)
				//{
				//TODO:give the option to tap on map to select the end location
					Address address=addresses.get(0);
					// i need to add another dialog box or table view here asking user to select from available choices
					Log.d("androLongitude","address returned " + address.getLatitude() + address.getLongitude());
					endLatitude = address.getLatitude();
					endLongitude = address.getLongitude();
					
				//}
			}
			else
			{
				Toast.makeText(this, "Location not found, please enter nearby location or landmark", Toast.LENGTH_LONG).show(); 
			}
			
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IllegalArgumentException e)
		{
			e.printStackTrace();
		}
		
		Intent i = new Intent(this,(Class<?>)(parentClass));
		double [] geopoint={0,0,0};
		geopoint[0] = endLatitude;
		geopoint[1] = endLongitude;
		geopoint[2] = distance;
		
		i.putExtra("endlocation", geopoint);
	
		startActivity(i);
		finish();
		
	}
	
		
		

}
